package pl.edu.pw.stud.ltrzaska.policy.xml;

import java.util.ArrayList;
import java.util.List;

import pl.edu.pw.stud.ltrzaska.policy.entity.Directory;
import pl.edu.pw.stud.ltrzaska.policy.entity.Filesystem;
import pl.edu.pw.stud.ltrzaska.policy.entity.FilesystemObject;
import pl.edu.pw.stud.ltrzaska.policy.entity.FilesystemTime;
import pl.edu.pw.stud.ltrzaska.policy.entity.Group;
import pl.edu.pw.stud.ltrzaska.policy.entity.PolicyValidator;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class DirectoryConverter implements Converter {

	@Override
	public boolean canConvert(Class clazz) {
		return (clazz == Group.class);
	}

	@Override
	public void marshal(Object arg0, HierarchicalStreamWriter arg1, MarshallingContext arg2) {
		// TODO Auto-generated method stub
	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		Directory dir = new Directory();

		ConverterCommons.preLoopUnmarshal(reader, dir);
		while (reader.hasMoreChildren()) {
			reader.moveDown();
			String attrName = reader.getNodeName();

			if ("children".equals(attrName)) {
				dir.setChildren((ArrayList<FilesystemObject>) context.convertAnother(dir, List.class));
			}
			ConverterCommons.inLoopUnmarshal(reader, context, dir);
						
			reader.moveUp();
		}
		return dir;
	}
}
